package com.bio.action.call;

import java.util.Calendar;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.bio.action.AbstractAction;
import com.bio.entity.call.Call;
import com.bio.entity.call.History;
import com.bio.entity.user.User;
import com.bio.service.call.CallService;
import com.bio.service.call.HistoryService;
import com.bio.service.user.UserService;
import com.bio.utils.Environment;

@Namespace("/call")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/call/listCalls.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "call!list", type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/call/editCall.jsp", type = "dispatcher"),
		@Result(name = "detail", location = "/WEB-INF/views/call/listDetail.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/call/addCall.jsp", type = "dispatcher")
		})
public class CallAction extends AbstractAction<Call, CallService> {

	public CallAction(CallService service) {
		super(service);
	}

	private static final long serialVersionUID = -6780075178356548080L;

	private List<History> allHistorys;
	
	private HistoryService historyService;
	
    private List<User> allChargePersons;
	
	private UserService userService; 
	
	private Call call;

	public String list(){
		pagination.setCurrentIndex(this.getCurrentIndex());
		this.pager = this.service.findAllByPager(pagination);
		return "list";
	}
	
	public String add(){
		Long companyId = Environment.Session.getCompanyId();
		allChargePersons = this.userService.findPagerByCompanyId(null, companyId).getResult();
		return "add";
	}
	
	public String save(){
		Calendar now = Calendar.getInstance();
		this.call.setCreatedDate(now);
		
		this.service.save(this.call);
		return "tolist";
	}
	
	public String edit(){
		Long companyId = Environment.Session.getCompanyId();
		allChargePersons = this.userService.findPagerByCompanyId(null, companyId).getResult();
		this.call = this.service.get(id);
		return "edit";
	}
	
	public String update(){
        Call oldCall = this.service.get(call.getId());
		
		this.call.setUpdatedDate(Calendar.getInstance());
		this.call.setCreatedDate(oldCall.getCreatedDate());
		
		this.service.save(call);
		return "tolist";
	}
	
	public String listDetail(){
		this.call = this.service.get(id);
		this.allHistorys = this.historyService.findPagerByCallId(null, this.call.getId()).getResult();
		return "detail";
	}
	
	public List<History> getAllHistorys() {
		return allHistorys;
	}

	public void setAllHistorys(List<History> allHistorys) {
		this.allHistorys = allHistorys;
	}

	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}

	public Call getCall() {
		return call;
	}

	public void setCall(Call call) {
		this.call = call;
	}

	public List<User> getAllChargePersons() {
		return allChargePersons;
	}

	public void setAllChargePersons(List<User> allChargePersons) {
		this.allChargePersons = allChargePersons;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
